import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Req } from '@nestjs/common';
import { DeviceService } from './device.service';
import { DtoPipe } from '../../pipe/dto/dto.pipe';
import CreateDto from './dto/create.dto';
import { ReqAttrs } from '../../constant';
import { ResponseUtilService } from '@app/response-util';
import PageDto from './dto/page.dto';
import UpdateDto from './dto/update.dto';
import RegisterToAliDto from './dto/registerToAli.dto';
import BindMerchantDto from './dto/bindMerchant.dto';
import UnbnindMerchantDto from './dto/unbindMerchant.dto';
import SetParamsDto from './dto/setParams.dto';

@Controller('device')
export class DeviceController {
    constructor(
        private readonly service:DeviceService
    ){}


    @Post('page')
    async page(@Body(new DtoPipe({skipMissingProperties : true})) dto:PageDto, @Req() req) {
        //const memberId = req[ReqAttrs.MEMBER_ID]
        const result = await this.service.page(dto)

        return ResponseUtilService.successRes(result)

    }

    @Post('add')
    async add(@Body(new DtoPipe()) dto:CreateDto, @Req() req) {
        try {
            const r = await this.service.add(dto, req[ReqAttrs.MEMBER_ID])
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Put('update')
    async update(@Body(new DtoPipe()) dto:UpdateDto) {
        try {
            const r = await this.service.update(dto)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Delete('delete/:id')
    async del(@Param('id', ParseIntPipe) id:number, @Body('syncDelAli') syncDelAli:boolean) {
        if(id < 1) return null
        try {
            const r = await this.service.del(id, syncDelAli)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        } 
    }

    @Post('registerToAli')
    async registerToAli(@Body(new DtoPipe()) dto:RegisterToAliDto, @Req() req) {
        try {
            await this.service.manyDeviceRegisterToAli(dto.ids)
            return ResponseUtilService.successRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post('bindMerchant')
    async bindMerchant(@Body(new DtoPipe()) dto:BindMerchantDto, @Req() req) {
        try {
            await this.service.bindMerchant(dto.merchantId, dto.ids)
            return ResponseUtilService.successRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post('unbindMerchant')
    async unbindMerchant(@Body(new DtoPipe()) dto:UnbnindMerchantDto, @Req() req) {
        try {
            await this.service.unbindMerchant(dto.ids)
            return ResponseUtilService.successRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post("setParams")
    async setParams(@Body(new DtoPipe()) dto:SetParamsDto, @Req() req) {
        try {
            await this.service.setParams(dto, req[ReqAttrs.MERCHANT_ID])
            return ResponseUtilService.successRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }
}
